package	model

import (
	"context"
	"fileService/common/mysql"
	"fileService/model"
	"fmt"
)

type _FilesService struct {
	*_BaseMgr
}

// FilesService open func
func FilesService() *_FilesService {
	if mysql.DB() == nil {
		panic(fmt.Errorf("FilesService need init by db"))
	}
	ctx, cancel := context.WithCancel(context.Background())
	return &_FilesService{_BaseMgr: &_BaseMgr{DB: mysql.DB().Table("files"), isRelated: globalIsRelated,ctx:ctx,cancel:cancel,timeout:-1}}
}

// GetTableName get sql table name.获取数据库名字
func (obj *_FilesService) GetTableName() string {
	return "files"
}

// Get 获取
func (obj *_FilesService) Get() (result model.Files, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&result).Error
	
	return
}

// Gets 获取批量结果
func (obj *_FilesService) Gets() (results []*model.Files, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&results).Error
	
	return
}

//////////////////////////option case ////////////////////////////////////////////

// WithID id获取 
func (obj *_FilesService) WithID(id int64) Option {
	return optionFunc(func(o *options) { o.query["id"] = id })
}

// WithFileName file_name获取 文件名称
func (obj *_FilesService) WithFileName(fileName string) Option {
	return optionFunc(func(o *options) { o.query["file_name"] = fileName })
}

// WithBucket bucket获取 桶
func (obj *_FilesService) WithBucket(bucket string) Option {
	return optionFunc(func(o *options) { o.query["bucket"] = bucket })
}

// WithHash hash获取 哈希
func (obj *_FilesService) WithHash(hash string) Option {
	return optionFunc(func(o *options) { o.query["hash"] = hash })
}

// WithMd5 md5获取 md5
func (obj *_FilesService) WithMd5(md5 string) Option {
	return optionFunc(func(o *options) { o.query["md5"] = md5 })
}

// WithPath path获取 实际路径
func (obj *_FilesService) WithPath(path string) Option {
	return optionFunc(func(o *options) { o.query["path"] = path })
}

// WithCreateTime create_time获取 时间
func (obj *_FilesService) WithCreateTime(createTime int) Option {
	return optionFunc(func(o *options) { o.query["create_time"] = createTime })
}


// GetByOption 功能选项模式获取
func (obj *_FilesService) GetByOption(opts ...Option) (result model.Files, err error) {
	options := options{
		query: make(map[string]interface{}, len(opts)),
	}
	for _, o := range opts {
		o.apply(&options)
	}

	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&result).Error
	
	return
}

// GetByOptions 批量功能选项模式获取
func (obj *_FilesService) GetByOptions(opts ...Option) (results []*model.Files, err error) {
	options := options{
		query: make(map[string]interface{}, len(opts)),
	}
	for _, o := range opts {
		o.apply(&options)
	}

	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&results).Error
	
	return
}
//////////////////////////enume case ////////////////////////////////////////////


// GetFromID 通过id获取内容  
func (obj *_FilesService)  GetFromID(id int64) (result model.Files, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("id = ?", id).Find(&result).Error
	
	return
}

// GetBatchFromID 批量唯一主键查找 
func (obj *_FilesService) GetBatchFromID(ids []int64) (results []*model.Files, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("id IN (?)", ids).Find(&results).Error
	
	return
}
 
// GetFromFileName 通过file_name获取内容 文件名称 
func (obj *_FilesService) GetFromFileName(fileName string) (results []*model.Files, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("file_name = ?", fileName).Find(&results).Error
	
	return
}

// GetBatchFromFileName 批量唯一主键查找 文件名称
func (obj *_FilesService) GetBatchFromFileName(fileNames []string) (results []*model.Files, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("file_name IN (?)", fileNames).Find(&results).Error
	
	return
}
 
// GetFromBucket 通过bucket获取内容 桶 
func (obj *_FilesService) GetFromBucket(bucket string) (results []*model.Files, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("bucket = ?", bucket).Find(&results).Error
	
	return
}

// GetBatchFromBucket 批量唯一主键查找 桶
func (obj *_FilesService) GetBatchFromBucket(buckets []string) (results []*model.Files, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("bucket IN (?)", buckets).Find(&results).Error
	
	return
}
 
// GetFromHash 通过hash获取内容 哈希 
func (obj *_FilesService) GetFromHash(hash string) (results []*model.Files, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("hash = ?", hash).Find(&results).Error
	
	return
}

// GetBatchFromHash 批量唯一主键查找 哈希
func (obj *_FilesService) GetBatchFromHash(hashs []string) (results []*model.Files, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("hash IN (?)", hashs).Find(&results).Error
	
	return
}
 
// GetFromMd5 通过md5获取内容 md5 
func (obj *_FilesService) GetFromMd5(md5 string) (results []*model.Files, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("md5 = ?", md5).Find(&results).Error
	
	return
}

// GetBatchFromMd5 批量唯一主键查找 md5
func (obj *_FilesService) GetBatchFromMd5(md5s []string) (results []*model.Files, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("md5 IN (?)", md5s).Find(&results).Error
	
	return
}
 
// GetFromPath 通过path获取内容 实际路径 
func (obj *_FilesService) GetFromPath(path string) (results []*model.Files, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("path = ?", path).Find(&results).Error
	
	return
}

// GetBatchFromPath 批量唯一主键查找 实际路径
func (obj *_FilesService) GetBatchFromPath(paths []string) (results []*model.Files, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("path IN (?)", paths).Find(&results).Error
	
	return
}
 
// GetFromCreateTime 通过create_time获取内容 时间 
func (obj *_FilesService) GetFromCreateTime(createTime int) (results []*model.Files, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("create_time = ?", createTime).Find(&results).Error
	
	return
}

// GetBatchFromCreateTime 批量唯一主键查找 时间
func (obj *_FilesService) GetBatchFromCreateTime(createTimes []int) (results []*model.Files, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("create_time IN (?)", createTimes).Find(&results).Error
	
	return
}
 
 //////////////////////////primary index case ////////////////////////////////////////////
 
 // FetchByPrimaryKey primay or index 获取唯一内容
 func (obj *_FilesService) FetchByPrimaryKey(id int64 ) (result model.Files, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("id = ?", id).Find(&result).Error
	
	return
}
 

 

	

